﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using Warehouse.Web.AppCode;

namespace ImportModule
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog()
            {
                FileName = "Authority.config",
                Multiselect = false,
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.tbFilePath.Text = dialog.FileName;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            string filepath = this.tbFilePath.Text.Trim();
            if (string.IsNullOrEmpty(filepath))
            {
                MessageBox.Show("请选择文件");
            }
            else if (File.Exists(filepath))
            {
                try
                {
                    ExeConfigurationFileMap exeConfigMap = new ExeConfigurationFileMap();
                    exeConfigMap.ExeConfigFilename = filepath;
                    Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(exeConfigMap, ConfigurationUserLevel.None);
                    ConfigurationSection authoritySection = cfg.GetSection("authority");
                    if (authoritySection != null)
                    {
                        Dictionary<string, List<string>> modules = new Dictionary<string, List<string>>();

                        AuthorityCfg authorityCfg = (AuthorityCfg)authoritySection;
                        foreach (AuthorityItem item in authorityCfg.Authorities)
                        {
                            foreach (Warehouse.Web.AppCode.Action action in item.Actions)
                            {
                                string[] names = action.PmName.Split(new char[] { '-' });

                                if (!modules.ContainsKey(names[0]))
                                {
                                    modules.Add(names[0], new List<string>());
                                }
                                if (!modules[names[0]].Contains(names[1]))
                                {
                                    modules[names[0]].Add(names[1]);
                                }
                            }
                        }
                        foreach (var modulename in modules.Keys)
                        {
                            Warehouse.DataAccess.Module m = new Warehouse.DataAccess.Module()
                            {
                                Name = modulename,
                                ParentID = 0
                            };
                            m.Save();
                            foreach (var actionname in modules[modulename])
                            {
                                Warehouse.DataAccess.Permission p = new Warehouse.DataAccess.Permission()
                                {
                                    ModuleID = m.ID,
                                    ActionName = actionname,
                                };
                                p.Save();
                            }
                        }
                        MessageBox.Show("导入成功");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导入失败：\n" + ex.ToString());
                }
            }
            else
            {
                MessageBox.Show("文件不存在");
            }
        }
    }
}
